﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;

using iPhoneUI.Shell;

using Microsoft.WindowsMobile.Status;

namespace iPhoneUI.Widgets
{
    public class CalendarButtonDecorator : TextWidget
    {
        public CalendarButtonDecorator()
        {
            Font = ShellConfig.CreateFont(25.75F, FontStyle.Bold);
            DayFont = ShellConfig.CreateFont(7, FontStyle.Bold);
        }

        public Font DayFont { get; set; }

        protected override void OnSetOwner()
        {
            RegisterSystemStateChange(SystemProperty.Date);

            base.OnSetOwner();
        }

        public override void LayoutAsDecoration(Rectangle reference)
        {
            ClientArea = reference; 
        }

        protected override void OnDraw(Canvas canvas, Rectangle clipBounds)
        {
            using (StringFormat format = new StringFormat(StringFormatFlags.NoClip))
            using (Brush b = new SolidBrush(ForeColor))
            {
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Far;
                canvas.DrawString(DateTime.Now.Day.ToString(), Font, b, ClientArea, format);
            }

            using (StringFormat format = new StringFormat(StringFormatFlags.NoWrap))
            using (Brush b = new SolidBrush(Color.White))
            {
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Near;

                canvas.DrawString(CultureInfo.CurrentUICulture.DateTimeFormat.GetAbbreviatedDayName(DateTime.Now.DayOfWeek), DayFont, b, ClientArea, format);
            }
        }

        protected override void OnDispose()
        {
            DayFont.SafeDispose();

            base.OnDispose();
        }
    }
}
